﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.COMM;
using Mang24k99.Business.COMM;
using System.Reflection;

namespace Mang24k99.Web.TreeView
{
    public partial class TreeConfig : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel1.Visible = false;
                BindGridview();
                BindObject();
                BindTextField();
                BindValueField();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Text=="新增")
            {
                if (NameValidate()==false)
                {
                    return;
                }
                if (NullValidate()==false)
                {
                    return;
                }
                MKtCommTreeconfig config = new MKtCommTreeconfig();
                config.TreeNO = MKtCommTreeconfig.GetMaxNO() + 1;
                config.TreeName = txtTreeName.Text.Trim();
                config.TableName = ddlObj.SelectedValue;
                config.DisfiledName = ddlTextField.SelectedValue;
                config.FilednoName = ddlValueField.SelectedValue;
                config.IsValidate = Convert.ToInt32(ddlIsValidate.SelectedValue);
                config.InputTime = DateTime.Now;
                try
                {
                    config.Create();
                    string strMsg = "新增成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    PageClear();
                    BindGridview();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            else
            {
                int treeno = Convert.ToInt32(txtTreeNO.Text.Trim());
                MKtCommTreeconfig config = BKtCommTreeconfig.Find(treeno);
                config.TreeName = txtTreeName.Text.Trim();
                config.TableName = ddlObj.SelectedValue;
                config.DisfiledName = ddlTextField.SelectedValue;
                config.FilednoName = ddlValueField.SelectedValue;
                config.IsValidate = Convert.ToInt32(ddlIsValidate.SelectedValue);
                config.ModifyTime = DateTime.Now;
                try
                {
                    config.Update();
                    string strMsg = "修改成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    PageClear();
                    BindGridview();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (btnSave.Text=="新增")
            {
                PageClear();
            }
            else
            {
                int treeno = Convert.ToInt32(txtTreeNO.Text.Trim());
                MKtCommTreeconfig config = BKtCommTreeconfig.Find(treeno);
                InitData(config);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            PageClear();
        }
        /// <summary>
        /// 清空页面
        /// </summary>
        public void PageClear()
        {
            Panel1.Visible = false;
            txtTreeName.Text = "";
            txtTreeNO.Text = "";
            btnSave.Text = "新增";
        }
        /// <summary>
        /// 绑定列表
        /// </summary>
        public void BindGridview()
        {
            MKtCommTreeconfig[] treeconfiglist = (MKtCommTreeconfig[])BKtCommTreeconfig.FindAll();
            grdList.DataSource = treeconfiglist;
            grdList.DataBind();
        }
        /// <summary>
        /// 修改删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int treeno=Convert.ToInt32(e.CommandArgument.ToString());
            MKtCommTreeconfig config = BKtCommTreeconfig.Find(treeno);
            if (e.CommandName=="Modi")
            {
                Panel1.Visible = true;
                btnSave.Text = "更新";
                InitData(config);
            }
            if (e.CommandName=="Del")
            {
                try
                {
                    config.Delete();
                    string strMsg = "删除成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    PageClear();
                    BindGridview();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }

        private void InitData(MKtCommTreeconfig config)
        {
            txtTreeNO.Text = config.TreeNO.ToString();
            txtTreeName.Text = config.TreeName;
            ddlObj.SelectedValue = config.TableName;
            BindTextField();
            BindValueField();
            ddlTextField.SelectedValue = config.DisfiledName;
            ddlValueField.SelectedValue= config.FilednoName;
            ddlIsValidate.SelectedValue = config.IsValidate.ToString();
        }

        public string GetStatus(int isvalidate)
        {
            if (isvalidate==1)
            {
                return "使用";
            }
            else
            {
                return "禁用";
            }
        }
        /// <summary>
        /// 文本框是否为空验证
        /// </summary>
        /// <returns></returns>
        public bool NullValidate()
        {
            if (txtTreeName.Text.Trim()=="")
            {
                string strMsg = "请输入树名！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
          
            return true;
        }
        /// <summary>
        /// 树名是否存在验证
        /// </summary>
        /// <returns></returns>
        public bool NameValidate()
        {
            string treename=txtTreeName.Text.Trim();
            bool result = BKtCommTreeconfig.NameValidate(treename);
            if (result==false)
            {
                string strMsg = "该树名已经存在，请更换一个名称！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        public void BindObject()
        {
            Assembly a = SetAssembly();
            DataTable dt = GetObject(a);
            ddlObj.DataSource = dt;
            ddlObj.DataTextField = "Text";
            ddlObj.DataValueField = "Value";
            ddlObj.DataBind();

        }

        public void BindTextField()
        {
            string objFullName = ddlObj.SelectedValue;
            DataTable dt = GetField(objFullName);
            ddlTextField.DataSource = dt;
            ddlTextField.DataTextField = "Text";
            ddlTextField.DataValueField = "Value";
            ddlTextField.DataBind();
        }

        public void BindValueField()
        {
            string objFullName = ddlObj.SelectedValue;
            DataTable dt = GetField(objFullName);
            ddlValueField.DataSource = dt;
            ddlValueField.DataTextField = "Text";
            ddlValueField.DataValueField = "Value";
            ddlValueField.DataBind();
        }
        public Assembly SetAssembly()
        {
            Assembly a = Assembly.Load("Mang24k99.Model");
            return a;
        }
        /// <summary>
        /// 根据程序集查询对象
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public DataTable GetObject(Assembly a)
        {
            Type[] objArray = a.GetTypes();
            DataTable dt = new DataTable();
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("Value", typeof(string));
            foreach (Type obj in objArray)
            {
                string text = obj.Name;
                string value = obj.FullName;
                dt.Rows.Add(new object[] { text, value });
            }
            return dt;
        }

        /// <summary>
        /// 根据对象名查找属性
        /// </summary>
        /// <param name="objFullName"></param>
        /// <returns></returns>
        public DataTable GetField(string objFullName)
        {
            Assembly a = SetAssembly();
            Type obj = a.GetType(objFullName);
            PropertyInfo[] pro = obj.GetProperties();
            DataTable dt = new DataTable();
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("Value", typeof(string));

            foreach (PropertyInfo p in pro)
            {
                string text = p.Name;
                string value = p.Name;
                dt.Rows.Add(new object[] { text, value });
            }
            return dt;
        }

        protected void ddlObj_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindValueField();
            BindTextField();
        }

        public string SubString(string tablename)
        {
            return tablename.Substring(tablename.LastIndexOf(".")+1);
        }
    }
}
